package com.dzwww.recommend.rule.controller;


import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.GlobalConfig;
import com.baomidou.mybatisplus.generator.config.PackageConfig;
import com.baomidou.mybatisplus.generator.config.StrategyConfig;
import com.baomidou.mybatisplus.generator.config.converts.MySqlTypeConvert;
import com.baomidou.mybatisplus.generator.config.rules.DbColumnType;
import com.baomidou.mybatisplus.generator.config.rules.IColumnType;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import org.apache.commons.lang3.StringUtils;

/**
 * com.dzwww.media.CodeGenerator
 * <p>
 *
 * @author lixin
 * @version 1.0
 * @date 2018/11/27 1:46 PM
 */
public class CodeGenerator {

    /**
     * 公司包名
     */
    private static final String COMPANY_PACKAGE = "com.dzwww";
    /**
     * 项目包名
     */
    private static final String PROJECT_PACKAGE = "recommend.rule";
    /**
     * dao模块路径
     */
    private static final String DAO_MODULE_NAME = "code";

    /**
     * 数据库表前缀
     */
    private static final String[] TABLE_PREFIX = new String[]{"dzwww_"};

    /**
     * 代码注释作者
     */
    private static final String AUTHOR = "Auto";

    private static final String DB_HOST = "119.3.184.162";
    private static final String DB_PORT = "3306";

    /**
     * 数据库名
     */
    private static final String DB_DATABASE = "dzrule";
    private static final String DB_USERNAME = "root";
    private static final String DB_PASSWORD = "Dzw@DB2019!";
    private static final String DB_URL = "jdbc:mysql://" + DB_HOST + ":" + DB_PORT + "/" + DB_DATABASE
            + "?serverTimezone=GMT%2B8&useUnicode=true&characterEncoding=UTF8&serverTimezone=UTC&useSSL=false";

    public static void main(String[] args) {
        // 代码生成器
        AutoGenerator mpg = new AutoGenerator();

        // 全局配置
        GlobalConfig gc = new GlobalConfig();
        String projectPath = System.getProperty("user.dir");
        gc.setOutputDir(projectPath + "/" + DAO_MODULE_NAME + "/src/test/java")
                // XML ResultMap
                .setBaseResultMap(true)
                // XML columList
                .setBaseColumnList(true)
                .setAuthor(AUTHOR)
                .setOpen(false)
                // 自定义文件命名，注意 %s 会自动填充表实体属性！
                .setMapperName("%sMapper")
                .setXmlName("%sMapper")
                .setServiceName("%sService")
                .setServiceImplName("%sServiceImpl")
                .setControllerName("%sController");
        mpg.setGlobalConfig(gc);

        // 数据源配置
        DataSourceConfig dsc = new DataSourceConfig();
        dsc.setUrl(DB_URL)
                .setDriverName("com.mysql.jdbc.Driver")
                .setUsername(DB_USERNAME)
                .setPassword(DB_PASSWORD)
                .setTypeConvert(new MySqlTypeConvert() {
                    /**
                     * 自定义数据库表字段类型转换【可选】
                     *
                     * @param globalConfig globalConfig
                     * @param fieldType    数据库字段类型
                     * @return IColumnType
                     */
                    @Override
                    public IColumnType processTypeConvert(GlobalConfig globalConfig, String fieldType) {
                        if (StringUtils.containsIgnoreCase(fieldType, "datetime")) {
                            return DbColumnType.DATE;
                        } else if (StringUtils.containsIgnoreCase(fieldType, "decimal")) {
                            return DbColumnType.BIG_DECIMAL;
                        } else {
                            return super.processTypeConvert(globalConfig, fieldType);
                        }
                    }
                });
        mpg.setDataSource(dsc);

        // 包配置
        PackageConfig pc = new PackageConfig();
        pc.setParent(COMPANY_PACKAGE)
                .setModuleName(PROJECT_PACKAGE)
                .setEntity("common.po")
                .setMapper("dao.mapper")
                .setXml("dao.xml")
                .setService("service.common")
                .setServiceImpl("service.common.impl")
                .setController("controller");
        mpg.setPackageInfo(pc);

        // 策略配置
        StrategyConfig strategy = new StrategyConfig();
        strategy.setNaming(NamingStrategy.underline_to_camel)
                .setColumnNaming(NamingStrategy.underline_to_camel)
                .setEntityTableFieldAnnotationEnable(false)
                .setSuperEntityClass(COMPANY_PACKAGE + "." + PROJECT_PACKAGE + ".common.po.base.BaseLogicDeleteEntity")
                .setSuperServiceClass(COMPANY_PACKAGE + "." + PROJECT_PACKAGE + ".service.base.BaseService")
                .setSuperServiceImplClass(COMPANY_PACKAGE + "." + PROJECT_PACKAGE + ".service.base.impl.BaseServiceImpl")
                .setEntityLombokModel(true)
                .setRestControllerStyle(true)
                // 需要生成的表
                //strategy.setInclude(new String[]{""});
                // 排除生成的表
                .setExclude(new String[]{"base"})
                // 自定义实体，公共字段
                .setSuperEntityColumns(new String[]{"id", "feature", "status", "deleted", "gmt_create", "gmt_modified"})
                .setControllerMappingHyphenStyle(true)
                // 表前缀
                .setTablePrefix(TABLE_PREFIX);
        mpg.setStrategy(strategy);
        mpg.execute();
    }

}
